package cn.iocoder.yudao.module.ems.service.maintainstandard;

import cn.iocoder.yudao.module.ems.controller.admin.maintainstandard.vo.MaintainStandardItemRespVO;
import cn.iocoder.yudao.module.ems.convert.maintainstandard.MaintainStandardItemConvert;
import cn.iocoder.yudao.module.ems.dal.dataobject.maintainstandard.MaintainStandardItemDO;
import cn.iocoder.yudao.module.ems.dal.mysql.maintainstandard.MaintainStandardItemMapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.List;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.ems.enums.ErrorCodeConstants.WAREHOUSING_NOT_EXISTS;

/**
 * 入库信息 Service 实现类
 *
 * @author dason
 */
@Service
@Validated
public class MaintainStandardItemServiceImpl implements MaintainStandardItemService {

    @Resource
    private MaintainStandardItemMapper maintainStandardItemMapper;

    private void validateMaintainStandardItemExists(Long id) {
        if (maintainStandardItemMapper.selectById(id) == null) {
            throw exception(WAREHOUSING_NOT_EXISTS);
        }
    }

    @Override
    public List<MaintainStandardItemDO> getMaintainStandardItemList(Long warehousingId) {
        return maintainStandardItemMapper.selectList(warehousingId);
    }

    @Override
    public void deleteMaintainStandardItem(Long id) {
        // 校验存在
        this.validateMaintainStandardItemExists(id);
        // 删除
        maintainStandardItemMapper.deleteById(id);
    }


    @Override
    public void updateMaintainStandardItem(MaintainStandardItemRespVO updateReqVO) {
        // 校验存在
        this.validateMaintainStandardItemExists(updateReqVO.getId());
        // 更新
        MaintainStandardItemDO updateObj = MaintainStandardItemConvert.INSTANCE.convert3(updateReqVO);
        maintainStandardItemMapper.updateById(updateObj);
    }

}
